﻿<?php
class v1_advertise{
    function index(){
		$data['basic']=$this->yum->config['Manage']['domain'].'?c=advertise&a=index';
		$params=array();
		if($this->yum->get('order',true)){
			$data['order']=$this->yum->get('order',true);$data['rule']=$this->yum->get('rule',true);
			if($data['order']=='time'&&in_array($data['rule'],array('desc','asc'))){
				$params['order']=array($data['order']=>$data['rule']);
			}
		}else{
			$data['order']='time';$data['rule']='desc';
			$params['order']=array('time'=>'desc');
		}
		if($this->yum->get('date',true)){
			$data['date']=explode(',',$this->yum->get('date',true));
			if(count($data['date'])==2){
				$params['where']=array('date >='=>strtotime($data['date'][0]),'date <='=>strtotime($data['date'][1]));
				$data['basic'].='&date='.$data['date'][0].','.$data['date'][1];
			}
		}
		if($this->yum->get('locked',true)){
			$temp=$this->yum->get('locked',true);
			if(is_array($temp)){
				foreach($temp as $key=>$value){
					$data['locked'][$value]=intval($value);
					$data['basic'].='&locked[]='.$value;
				}
				$params['where']['locked IN']=$data['locked'];
			}
		}
		if($this->yum->get('search',true)){
			$data['search']=urldecode($this->yum->get('search',true));$data['tab']=$this->yum->get('tab',true);
			if($data['search']!=''){
				if($data['tab']=='ID'){
					$params['where']['id =']=intval($data['search']);
				}else if($data['tab']=='name'){
					$params['where']['name LIKE']='%'.$data['search'].'%';
				}
			}
		}
		$config=$this->yum->model('config');
		$data['material']=$config->GConfig(array('where'=>array('label ='=>'advertiser','name='=>'material')));
		$data['material']=reset($data['material']);$data['material']=$data['material']['value'];
		$ad=$this->yum->model('advertisers');
		$data['total']=$ad->GAd(array('select'=>'COUNT(*) AS count','where'=>$params['where']));
		$data['total']=empty($data['total'])?0:$data['total'][0]['count'];
		$params['limit']=array(intval($this->yum->get('start')),15);
		$data['advertises']=$ad->GAd($params);
		$sort=$this->yum->model('sort');
		$data['sort']=$sort->GSort(array('select'=>'id,name,value,parent','where'=>array('label IN'=>array('modality','format'))));
		$pagination=$this->yum->library('pagination',array('query_string'=>'start','base_url'=>$data['basic'].'&search='.urlencode($data['search']).'&order='.$data['order'].'&rule='.$data['rule'],'total_rows'=>$data['total'],'per_page'=>15,'first_link'=>'首页','last_link'=>'末页','cur_tag_open'=>'<a href="#" class="active">','cur_tag_close'=>'</a>'));
		$data['pagination']=$pagination->create_links();
		$this->yum->set($data);
    	$this->yum->view('index.php');
    }
    function tactic(){
    	$ad=$this->yum->model('advertisers');
    	if($this->yum->post('save')){
    		$advertise=intval($this->yum->post('advertise'));$tactic['china']=array_map('intval',$this->yum->post('china'));
    		$tactic['adcolumn']=array_map('intval',$this->yum->post('adcolumn'));$tactic['keyword']=$this->yum->post('keyword');
			$affected=$ad->UAd(array('set'=>array('tactic='=>serialize($tactic)),'where'=>array('id='=>$advertise)));
			if($affected==1){
				$output['result']=1;
				$output['msg']='修改成功.';
			}else{
				$output['msg']='请误提交相同或未经修改的数据.';
			}
			header('Content-Type:text/plain');
			echo json_encode($output);
    	}else{
	    	$data['advertiser']=intval($this->yum->get('advertiser'));
	    	$data['tactic']=reset($ad->GAd(array('select'=>'tactic','where'=>array('id='=>$data['advertiser']))));
	    	if(empty($data['tactic']['tactic'])){
	    		$data['tactic']=array('china'=>array(),'adcolumn'=>array());
	    	}else{
	    		$data['tactic']=unserialize($data['tactic']['tactic']);
	    	}
			$sort=$this->yum->model('sort');
			$data['current']=array();
			$data['china']=$sort->SortTree(array('where'=>array('label='=>'china','locked='=>0)));
			$data['adcolumn']=$sort->SortTree(array('where'=>array('label='=>'adcolumn','locked='=>0)));
			$this->yum->set($data);
			$this->yum->view('tactic.php');
    	}
    }
    function append(){
    	$action=$this->yum->get('action');
    	if($action=='upload'){
    		$output=array('result'=>1,'msg'=>'');
    		$directory=$this->yum->vars('app','material');
    		$httpupload=$this->yum->library('httpupload',array('max'=>1024*1024*10,'limit'=>array('jpg','png','gif','html','htm','swf'),'directory'=>$directory));
			$httpupload->ini(array('level'=>true));
			$result=$httpupload->saveAs(str_shuffle('ABCDEF0123456789'),'material');
			if($result!==false){
				$output['result']=0;
				$output['material']=str_replace($directory,'',$result);
			}else{
				$output['msg']=$httpupload->errors();
			}
			header('Content-Type:text/plain');
			echo json_encode($output);
    	}else{
			if($this->yum->post('save')){
				$session=$this->yum->library('session');
				$output=array('result'=>0,'msg'=>'');
				$name=$this->yum->post('name',true);$link=$this->yum->post('link',true);$material=$this->yum->post('material',true);
				$format=$this->yum->post('format',true);$modality=$this->yum->post('modality',true);$online=intval($this->yum->post('online'));
				if(preg_match("/^http(s)*:\/\/[a-zA-Z0-9]+(\.[a-zA-Z0-9]+)+(\/.*)*$/u",$link)==0){
					$output['msg']='请输入一个正确的链接地址.';
				}else if(empty($material)){
					$output['msg']='素材文件不可为空.';
				}else if($modality==0){
					$output['msg']='素材尺寸不可为空.';
				}else if($format==0){
					$output['msg']='请选择素材格式.';
				}else{
					$ad=$this->yum->model('advertisers');
					$affected=$ad->IAd(array('name'=>$name,'link'=>urlencode($link),'material'=>$material,'format'=>$format,'modality'=>$modality,'online'=>$online,'member'=>$session->GVAR('member')));
					if($affected==1){
						$output['result']=2;
						$output['msg']='添加成功.';
					}else{
						$output['msg']='请误提交相同或未经修改的数据.';
					}
				}
				header('Content-Type:text/plain');
				echo json_encode($output);
			}else{
				$data['session']=array($this->yum->vars('lib','session','name'),$_COOKIE[$this->yum->vars('lib','session','name')]);
				$sort=$this->yum->model('sort');
				$data['modality']=$sort->SortTree(array('where'=>array('label='=>'modality','locked='=>0)));
				$data['format']=$sort->GSort(array('where'=>array('label='=>'format','locked='=>0)));
				$this->yum->set($data);
				$this->yum->view('append.php');
			}
    	}
	}
	function update(){
		$output=array('result'=>0,'msg'=>'');
		$config=$this->yum->model('config');
		$init=reset($config->GConfig(array('where'=>array('label ='=>'advertiser','name='=>'init'))));
		$advertiser=$this->yum->config('advertiser');
		$encryption=$this->yum->vars('app','encryption');
		$init=explode(',',$init['value']);
		$SERVER_ADDR=$this->yum->vars('app','server');
		if(empty($SERVER_ADDR)){
			$SERVER_ADDR=$this->yum->server('SERVER_ADDR');
		}
		$security=md5($encryption.$SERVER_ADDR);
		$context=stream_context_create(array('http'=>array('method'=>'GET','timeout'=>1)));
		foreach($init as $value){
			$response=@file_get_contents($value.$advertiser['app']['advertiser'].'.init.php?security='.$security,null,$context);
			if($response==='SUCCESS'){
				$output['msg'].=$value.':SUCCESS'."\n";
			}else{
				$output['msg'].=$value.':ERROR'."\n";
			}
		}
		header('Content-Type:text/plain');
		echo json_encode($output);
	}
	function edit(){
		$action=$this->yum->get('action');
    	if($action=='upload'){
    		$output=array('result'=>1,'msg'=>'');
    		$directory=$this->yum->vars('app','material');
    		$httpupload=$this->yum->library('httpupload',array('max'=>1024*1024,'limit'=>array('jpg','png','gif','html','htm','swf'),'directory'=>$directory));
			$httpupload->ini(array('level'=>true));
			$result=$httpupload->saveAs(str_shuffle('ABCDEF0123456789'),'material');
			if($result!==false){
				$output['result']=0;
				$output['material']=str_replace($directory,'',$result);
			}else{
				$output['msg']=$httpupload->errors();
			}
			header('Content-Type:text/plain');
			echo json_encode($output);
    	}else{
			$advertiser=$this->yum->model('advertisers');
			if($this->yum->post('save')){
				$output=array('result'=>0,'msg'=>'');
				$id=intval($this->yum->post('id'));
				$name=$this->yum->post('name',true);$link=$this->yum->post('link',true);$material=$this->yum->post('material',true);
				$format=$this->yum->post('format',true);$modality=$this->yum->post('modality',true);$online=intval($this->yum->post('online'));
				if(preg_match("/^http(s)*:\/\/[a-zA-Z0-9]+(\.[a-zA-Z0-9]+)+(.*)*$/u",$link)==0){
					$output['msg']='请输入一个正确的链接地址.';
				}else if(empty($material)){
					$output['msg']='素材文件不可为空.';
				}else if($modality==0){
					$output['msg']='素材尺寸不可为空.';
				}else if($format==0){
					$output['msg']='请选择素材格式.';
				}else{
					$ad=$this->yum->model('advertisers');
					$affected=$ad->UAd(array('set'=>array('name='=>$name,'link='=>urlencode($link),'material='=>$material,'format='=>$format,'modality='=>$modality,'online='=>$online),'where'=>array('id='=>$id)));
					if($affected==1){
						$output['result']=2;
						$output['msg']='修改成功.';
					}else{
						$output['msg']='请误提交相同或未经修改的数据.';
					}
				}
				header('Content-Type:text/plain');
				echo json_encode($output);
			}else{
				$data['session']=array($this->yum->vars('lib','session','name'),$_COOKIE[$this->yum->vars('lib','session','name')]);
				$data['advertiser']=reset($advertiser->GAd(array('where'=>array('id='=>intval($this->yum->get('advertiser'))))));
				$sort=$this->yum->model('sort');
				$data['modality']=$sort->GSort(array('where'=>array('label='=>'modality','locked='=>0)));
				$data['advertiser']['modality']=$data['modality'][$data['advertiser']['modality']];
				$data['modality']=$sort->GTree($data['modality']);
				$data['format']=$sort->GSort(array('where'=>array('label='=>'format','locked='=>0)));
				$this->yum->set($data);
				$this->yum->view('edit.php');
			}
    	}
	}
	function online(){
		$output=array('result'=>0,'msg'=>'');
		$advertiser=$this->yum->model('advertisers');
		$id=$this->yum->get('advertise');
		$online=intval($this->yum->get('online',true));
		if(is_array($id)){
			$affected=$advertiser->UAd(array('where'=>array('id IN'=>$id),'set'=>array('online='=>$online)));
			if($affected==0){
				$output['msg']='重复的数据提交.';
			}else{
				$output['result']=2;
			}
		}else{
			$affected=$advertiser->UAd(array('where'=>array('id='=>$id),'set'=>array('online='=>$online)));
			if($affected==1){
				$output['result']=5;
				$output['td']=array('offset'=>7,'data'=>array($online==0?'否':'是'));
				$output['exec']=array('attr'=>array('mixed'=>'sub:{online:'.($online==0?1:0).'}'),'text'=>($online==0?'投放':'停止'));
			}else{
				$output['msg']='广告状态已变更.';
			}
		}
		header('Content-Type:text/plain');
		echo json_encode($output);
	}
}
?>